package com.ruoyi.examation.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.examation.vo.ExamScheduleVo;
import com.ruoyi.examation.domain.ExamRegistrationUser;
import com.ruoyi.examation.service.IExamScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 考试时间安排Controller
 *
 * @author Tellsea
 * @date 2025-10-10
 */
@Api(tags = "考试时间安排Controller")
@RestController
@RequestMapping("/business/examSchedule")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamScheduleController extends BaseController {

    private final IExamScheduleService examScheduleService;

    @ApiOperation("查询考试时间安排列表")
    @PreAuthorize("@ss.hasPermi('business:examSchedule:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamScheduleVo> list(ExamScheduleVo entity) {
        return examScheduleService.queryList(entity);
    }

    @ApiOperation("查询考试时间安排所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamScheduleVo entity) {
        return AjaxResult.success("查询成功", examScheduleService.queryAll(entity));
    }

    @ApiOperation("导出考试时间安排列表")
    @PreAuthorize("@ss.hasPermi('business:examSchedule:export')")
    @Log(title = "考试时间安排", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamScheduleVo entity) {
        List<ExamScheduleVo> list = examScheduleService.queryAll(entity);
        ExcelUtil<ExamScheduleVo> util = new ExcelUtil<>(ExamScheduleVo.class);
        util.exportExcel(response, list, "考试时间安排数据");
    }

    @ApiOperation("获取考试时间安排详细信息")
    @PreAuthorize("@ss.hasPermi('business:examSchedule:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", examScheduleService.queryById(id));
    }

    @ApiOperation("新增考试时间安排")
    @PreAuthorize("@ss.hasPermi('business:examSchedule:add')")
    @Log(title = "考试时间安排", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamScheduleVo entity) {
        return toAjax(examScheduleService.save(entity));
    }

    @ApiOperation("修改考试时间安排")
    @PreAuthorize("@ss.hasPermi('business:examSchedule:edit')")
    @Log(title = "考试时间安排", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamScheduleVo entity) {
        return toAjax(examScheduleService.updateById(entity));
    }

    @ApiOperation("删除考试时间安排")
    @PreAuthorize("@ss.hasPermi('business:examSchedule:remove')")
    @Log(title = "考试时间安排", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examScheduleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("根据考试ID查询启用的考试时段并统计报名人数")
    @GetMapping("/listByExamIdWithCount/{examId}")
    public AjaxResult listByExamIdWithCount(@PathVariable("examId") Long examId) {
        List<ExamScheduleVo> schedules = examScheduleService.queryEnabledSchedulesWithRegistrationCount(examId);
        return AjaxResult.success("查询成功", schedules);
    }

    @ApiOperation("导出考试人员名单")
    @Log(title = "考试人员名单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportRegistrationList")
    public void exportRegistrationList(HttpServletResponse response, ExamScheduleVo entity) {
        List<ExamRegistrationUser> list = examScheduleService.getExamRegistrationList(entity.getExamId());
        ExcelUtil<ExamRegistrationUser> util = new ExcelUtil<>(ExamRegistrationUser.class);
        util.exportExcel(response, list, "考试人员报名列表");
    }

}
